package com.zkt.recommend.domain.service.impl;

import com.zkt.recommend.common.utils.LoginUtil;
import com.zkt.recommend.domain.algorithm.RecommendationAlgorithm;
import com.zkt.recommend.domain.service.RecommendDomainService;
import com.zkt.recommend.infra.basic.entity.RecommendUserPortrait;
import com.zkt.recommend.infra.basic.entity.SubjectInfo;
import com.zkt.recommend.infra.basic.service.IRecommendUserPortraitService;
import com.zkt.recommend.infra.entity.SubjectLabelInfo;
import com.zkt.recommend.infra.rpc.SubjectRPC;
import com.zkt.recommend.infra.rpc.UserRPC;
import com.zkt.subject.entity.SubjectInfoDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/27
 * @description 推荐service层实现类
 **/

@Service
public class RecommendDomainServiceImpl implements RecommendDomainService {
	
	@Resource
	private IRecommendUserPortraitService recommendUserPortraitService;
	
	@Resource
	private RecommendationAlgorithm recommendationAlgorithm;
	
	@Resource
	private SubjectRPC subjectRPC;
	
	@Resource
	private UserRPC userRPC;
	
	/**
	 * 获取推荐题目
	 *
	 * @param labelIds 推荐题目标签列表
	 * @param hasLabel 用户是否选择了标签
	 * @param num
	 * @return 推荐的题目
	 */
	@Override
	public List<SubjectInfoDTO> getRecommendSubject(List<Long> labelIds, boolean hasLabel, Integer num) {
		if (!hasLabel) {
			List<SubjectLabelInfo> userLabelList = subjectRPC.getUserLabels(null);
			labelIds = userLabelList.stream().map(SubjectLabelInfo::getId).collect(Collectors.toList());
		}
		
		// 获取用户画像
		List<RecommendUserPortrait> recommendUserPortraitList = recommendUserPortraitService.lambdaQuery()
				.eq(RecommendUserPortrait::getUserId, userRPC.getUserInfo(LoginUtil.getLoginId()).getId())
				.in(RecommendUserPortrait::getLabelId, labelIds)
				.list();
		
		// 获取题目列表
		List<SubjectInfoDTO> subjectInfoList = subjectRPC.getSubjectsByLabelIds(labelIds);
		
		return recommendationAlgorithm.recommend(recommendUserPortraitList, subjectInfoList, num);
	}
}